package org.jeecg.modules.articleController;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.articleManagement.entity.ArticleContent;
import org.jeecg.common.modules.articleManagement.entity.ArticleDirectory;
import org.jeecg.common.modules.articleManagement.entity.ArticleManagement;
import org.jeecg.common.modules.articleManagement.service.IArticleContentService;
import org.jeecg.common.modules.articleManagement.service.IArticleDirectoryService;
import org.jeecg.common.modules.articleManagement.service.IArticleManagementService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.UUID;

/**
 * @Description: 文章管理
 * @Author: jeecg-boot
 * @Date:   2023-04-21
 * @Version: V1.0
 */
@Api(tags="文章管理")
@RestController
@RequestMapping("/articleManagement/articleManagement")
@Slf4j
public class ArticleManagementController extends JeecgController<ArticleManagement, IArticleManagementService> {
	@Autowired
	private IArticleManagementService articleManagementService;
	 @Autowired
	 private IArticleDirectoryService articleDirectoryService;
	 @Autowired
	 private IArticleContentService articleContentService;
	/**
	 * 分页列表查询
	 *
	 * @param articleManagement
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "文章管理-分页列表查询")
	@ApiOperation(value="文章管理-分页列表查询", notes="文章管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ArticleManagement articleManagement,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ArticleManagement> queryWrapper = QueryGenerator.initQueryWrapper(articleManagement, req.getParameterMap());
		Page<ArticleManagement> page = new Page<ArticleManagement>(pageNo, pageSize);
        queryWrapper.orderByDesc("create_time");
		IPage<ArticleManagement> pageList = articleManagementService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param articleManagement
	 * @return
	 */
	@AutoLog(value = "文章管理-添加")
	@ApiOperation(value="文章管理-添加", notes="文章管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ArticleManagement articleManagement) {
		String id = UUID.randomUUID().toString();
		id = id.replace("-","");
		articleManagement.setId(id);
		articleManagement.setTypeShow(typeShow(articleManagement.getType()));
		articleManagementService.save(articleManagement);

		//添加目录
		ArticleDirectory articleDirectory = new ArticleDirectory();
		articleDirectory.setId(id);
		articleDirectory.setAuthor(articleManagement.getAuthor());
		articleDirectory.setBrief(articleManagement.getBrief());
		articleDirectory.setTitle(articleManagement.getTitle());
		articleDirectory.setThumbnailPic(articleManagement.getThumbnailPic());
		articleDirectory.setType(articleManagement.getType());
		articleDirectory.setTypeShow(articleManagement.getType());
		articleDirectoryService.save(articleDirectory);

		//添加文章内容
		ArticleContent articleContent = new ArticleContent();
		articleContent.setId(id);
		articleContent.setContent(articleManagement.getContent());
		articleContent.setDoctor(articleManagement.getDoctor());
		articleContent.setDoctorId(articleManagement.getDoctorId());
		articleContent.setVideo(articleManagement.getVideo());
		articleContent.setTitle(articleManagement.getTitle());
		articleContentService.save(articleContent);

		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param articleManagement
	 * @return
	 */
	@AutoLog(value = "文章管理-编辑")
	@ApiOperation(value="文章管理-编辑", notes="文章管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ArticleManagement articleManagement) {
		articleManagement.setId(articleManagement.getId());
		articleManagement.setTypeShow(typeShow(articleManagement.getType()));
		articleManagementService.saveOrUpdate(articleManagement);

		//添加目录
		ArticleDirectory articleDirectory = new ArticleDirectory();
		articleDirectory.setId(articleManagement.getId());
		articleDirectory.setAuthor(articleManagement.getAuthor());
		articleDirectory.setBrief(articleManagement.getBrief());
		articleDirectory.setTitle(articleManagement.getTitle());
		articleDirectory.setThumbnailPic(articleManagement.getThumbnailPic());
		articleDirectory.setTypeShow(articleManagement.getType());
		articleDirectoryService.saveOrUpdate(articleDirectory);

		//添加文章内容
		ArticleContent articleContent = new ArticleContent();
		articleContent.setId(articleManagement.getId());
		articleContent.setContent(articleManagement.getContent());
		articleContent.setDoctor(articleManagement.getDoctor());
		articleContent.setDoctorId(articleManagement.getDoctorId());
		articleContent.setVideo(articleManagement.getVideo());
		articleContentService.saveOrUpdate(articleContent);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "文章管理-通过id删除")
	@ApiOperation(value="文章管理-通过id删除", notes="文章管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		articleManagementService.removeById(id);
		articleDirectoryService.removeById(id);
		articleContentService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "文章管理-批量删除")
	@ApiOperation(value="文章管理-批量删除", notes="文章管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.articleManagementService.removeByIds(Arrays.asList(ids.split(",")));
		this.articleDirectoryService.removeByIds(Arrays.asList(ids.split(",")));
		this.articleContentService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "文章管理-通过id查询")
	@ApiOperation(value="文章管理-通过id查询", notes="文章管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ArticleManagement articleManagement = articleManagementService.getById(id);
		if(articleManagement==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(articleManagement);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param articleManagement
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ArticleManagement articleManagement) {
        return super.exportXls(request, articleManagement, ArticleManagement.class, "文章管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ArticleManagement.class);
    }
    //0生育，2儿童，3体重，4睡眠，5脊柱
    private String typeShow(String type){
    	String typeShow = "";
    	switch (type){
			case "0":
				 typeShow = "生育";
				 break;
			case "2":
				 typeShow = "儿童";
				 break;
			case "3":
				typeShow = "体重";
				break;
			case "4":
				typeShow = "睡眠";
				break;
			case "5":
				typeShow = "脊柱";
				break;
			case "6":
				typeShow = "老年健康管理";
				break;
		}
		return typeShow;
	}

}
